package dao.book;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import entity.BookType;
import utils.ComonUtil;
import utils.SqlConnection;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 
 *
 */
public class BookTypeDaoImpl implements BookTypeDao {

	@Override
	public List<BookType> getBookTypeList(Connection con, BookType bookType) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement pstm = null;
		ResultSet res = null;
		List<BookType> bookTypeList = new ArrayList<BookType>();
		if (con != null) {
			StringBuffer stringBuffer = new StringBuffer();
			stringBuffer.append("select * from booktype ");
			ArrayList<Object> paramList = new ArrayList<Object>();
			if (!ComonUtil.isEmpty(bookType.getBookTypeName())) {
				paramList.add("%" + bookType.getBookTypeName() + "%");
				stringBuffer.append("and bookTypeName like ? ");
			}
			if (!ComonUtil.isEmpty(bookType.getBookTypeDesc())) {
				paramList.add("%" + bookType.getBookTypeDesc() + "%");
				stringBuffer.append("and bookTypeDesc like ? ");// 模糊查询
			}
			
			if(bookType.getId()!=-1) {
				paramList.add(bookType.getId());
				stringBuffer.append("and id = ? ");
			}
			
			String sql = stringBuffer.toString().replaceFirst("and", "where");
			System.out.println(sql);
			Object[] params = paramList.toArray();
			res = SqlConnection.executeQuery(con, pstm, res, sql, params);
			
			while(res.next()) {
				BookType bookType1 = new BookType();
				bookType1.setId(res.getInt("id"));
				bookType1.setBookTypeDesc(res.getString("bookTypeDesc"));
				bookType1.setBookTypeName(res.getString("bookTypeName"));
				bookTypeList.add(bookType1);
			}
			SqlConnection.closeConnection(con, pstm, res);
		}
		return bookTypeList;
	}

	@Override
	public int addBookType(Connection con, BookType bookType) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement pstm = null;
		int row = 0;
		
		if(con!=null) {
			String sql = "insert into booktype (bookTypeName,bookTypeDesc) values(?,?)";
			Object[] params = {
					bookType.getBookTypeName(),
					bookType.getBookTypeDesc()
			};
			
			con = SqlConnection.getConnection();
			row = SqlConnection.executeUpdate(con, pstm, sql, params);
			SqlConnection.closeConnection(con, pstm, null);
		}
		
		return row;
	}

	@Override
	public int delBookType(Connection con, int id) throws Exception {
		// TODO Auto-generated method stub

		PreparedStatement pstm = null;
		int row = 0 ;
		if(con!=null) {
			String sql = "delete from booktype where id = ?";
			Object[] params = {id};
			row = SqlConnection.executeUpdate(con, pstm, sql, params);
		}
		return row;
	}

	@Override
	public int upBookType(Connection con, BookType bookType) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement pstm =null;
		int row = 0;
		
		if(con!=null) {
			StringBuffer stringBuffer = new StringBuffer();
			stringBuffer.append("update booktype set ");
			ArrayList<Object> paramsList = new ArrayList<Object>();
			if (!ComonUtil.isEmpty(bookType.getBookTypeName())) {
				paramsList.add(bookType.getBookTypeName() );
				stringBuffer.append("bookTypeName = ?,");
			}
			if (!ComonUtil.isEmpty(bookType.getBookTypeDesc())) {
				paramsList.add(bookType.getBookTypeDesc());
				stringBuffer.append("bookTypeDesc = ?,");
			}

			
			String sql = stringBuffer.toString();
//			System.out.println(sql.substring(sql.length()-1));
			if(sql.substring(sql.length()-1).equals(",")) {
				sql = sql.substring(0,sql.length()-1);
			}
			sql+=" where id = ?";
			paramsList.add(bookType.getId());
			System.out.println("修改语句："+sql);
			
			Object[] params = paramsList.toArray();
			row = SqlConnection.executeUpdate(con, pstm, sql, params);
			SqlConnection.closeConnection(con, pstm, null);
		}
		
		
		return row;
	}

}
